<template>
  <div class="app-container">
    <page-tools
      search-txt="网站名称"
      add-txt="新增友链"
      @onAdd="onAddOrEdit(0)"
    />
    <el-table :data="list" border style="width: 100%">
      <el-table-column
        fixed
        type="index"
        label="序列"
        width="50"
        align="center"
      />
      <el-table-column prop="icon" label="网站图标" width="150" align="center">
        <template v-slot="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.icon"
            fit="fill"
          />
        </template>
      </el-table-column>
      <el-table-column prop="title" label="网站名称" align="center" />
      <el-table-column prop="describe" label="网站简介" align="center" />
      <el-table-column prop="url" label="网站链接" align="center">
        <template slot-scope="sope">
          <a :href="sope.row.url" target="_blank">{{ sope.row.url }}</a>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="申请时间" align="center">
        <template slot-scope="sope">
          {{ sope.row.createTime | getTime }}
        </template>
      </el-table-column>
      <el-table-column
        prop="status"
        label="审核状态"
        width="150"
        align="center"
      >
        <template v-slot="scope">
          <p>
            <el-switch
              v-model="scope.row.status"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
              disabled
            />
          </p>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="onAddOrEdit(scope.row)"
          >编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <PagePagination
      :page="pageParams.page"
      :page-size="pageParams.pageSize"
      :total="total"
      @update-page="onUpdatePage"
      @update-size="onUpdateSize"
    />

    <el-dialog :visible.sync="dialogVisible" :title="title + '友链'" center>
      <el-form label-width="100px" :model="form">
        <el-form-item label="用户头像" prop="icon">
          <image-upload ref="myStaffPhoto" @updata-url="onUpdataUrl" />
        </el-form-item>
        <el-form-item label="网站标题">
          <el-input v-model="form.title" />
        </el-form-item>
        <el-form-item label="网站简介">
          <el-input v-model="form.describe" />
        </el-form-item>
        <el-form-item label="网站链接">
          <el-input v-model="form.url" />
        </el-form-item>
        <el-form-item label="网站链接">
          <el-switch
            v-model="form.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="onClose">取 消</el-button>
        <el-button type="primary" @click="onSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getLinkListAPI,
  getLinkByIdAPI,
  addLinkAPI,
  putLinkAPI
} from '@/api/link'

export default {
  data() {
    return {
      list: [],
      pageParams: {
        page: 1,
        pageSize: 5
      },
      total: 0,
      dialogVisible: false,
      form: {},
      title: ''
    }
  },
  mounted() {
    this.getLinkList()
  },
  methods: {
    async getLinkList() {
      const res = await getLinkListAPI(this.pageParams)
      this.list = res.data.result
      this.total = res.data.total
    },
    onUpdatePage(page) {
      this.pageParams.page = page
      this.getLinkList()
    },
    onUpdateSize(size) {
      this.pageParams.pageSize = size
      this.getLinkList()
    },
    async onAddOrEdit(row) {
      this.dialogVisible = true
      if (row.id) {
        const res = await getLinkByIdAPI(row.id)
        this.form = res.data
        this.title = '编辑'
        this.$refs['myStaffPhoto'].fileList = [{ url: row.icon, upload: true }]
      } else {
        this.title = '新增'
      }
    },
    onUpdataUrl(url) {
      this.form.avatar = url
    },
    async onSubmit() {
      const fileList = this.$refs['myStaffPhoto'].fileList
      if (this.form.id) {
        await putLinkAPI(this.form.id, { ...this.form, icon: fileList[0].url })
        this.getLinkList()
        this.$message({
          message: '编辑友链成功',
          type: 'success'
        })
      } else {
        await addLinkAPI({ ...this.form, icon: fileList[0].url })
        this.getLinkList()
        this.$message({
          message: '新增友链成功',
          type: 'success'
        })
      }
      this.dialogVisible = false
    },
    onClose() {
      this.dialogVisible = false
      this.form = {}
      this.$refs['myStaffPhoto'].fileList = []
    }
  }
}
</script>

<style lang="scss" scoped></style>
